package com.tadpole.music.holder.home;

import android.app.Activity;
import android.content.Intent;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.tadpole.music.R;
import com.tadpole.music.adapter.base.BaseAdapter;
import com.tadpole.music.adapter.home.SchoolGradeAdapter;
import com.tadpole.music.bean.me.MemberCenterBean;
import com.tadpole.music.config.Constant;
import com.tadpole.music.holder.base.BaseViewHolder;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;
import com.tadpole.music.view.activity.home.OnlineExercisesActivity;
import com.tadpole.music.view.activity.login.LoginActivity;
import com.tadpole.music.view.activity.me.MemberCenterActivity;
import com.tadpole.music.view.activity.me.PayDetailActivity;
import com.tadpole.music.view.activity.question.SimulationPracticeActivity;
import com.tadpole.music.view.activity.question.SpecialExercisesActivity;
import com.tadpole.music.view.widget.XCRoundRectImageView;
import java.util.List;

/**
 * Created By 孙岩
 * on 2020/9/8
 */
public class SchoolListViewHolder extends BaseViewHolder {
    private View itemView;
    private RecyclerView recyclerView;
    private SchoolGradeAdapter adapter;
    private Activity activity;
    private XCRoundRectImageView ivImage;
    private List<MemberCenterBean.DataBean> list;
    private BaseAdapter.OnChildClickListener onChildClickListener;
    public SchoolListViewHolder(Activity activity, View itemView, List<MemberCenterBean.DataBean> list,  BaseAdapter.OnChildClickListener onChildClickListener) {
        super(itemView);
        this.activity = activity;
        this.itemView = itemView;
        this.list = list;
        this.onChildClickListener = onChildClickListener;
    }

    @Override
    public void init(final int position) {
        recyclerView = itemView.findViewById(R.id.recyclerView);
        ivImage = itemView.findViewById(R.id.ivImage);
        RequestOptions requestOptions = new RequestOptions()
                .placeholder(R.mipmap.nomal_icon)
                .error(R.mipmap.nomal_icon)
                .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(activity).load(Constant.IMAGE_HEAD+list.get(position).getThumb()).apply(requestOptions).into(ivImage);


        GridLayoutManager layoutManager = new GridLayoutManager(activity, 3);
        recyclerView.setLayoutManager(layoutManager);
        //创建适配器
        adapter = new SchoolGradeAdapter(activity,list.get(position).getGoods());
        //给RecyclerView设置适配器
        recyclerView.setAdapter(adapter);
        recyclerView.setNestedScrollingEnabled(false);

        adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int i) {
                if(Mutils.isNetworkAvailable()){
                    if(SpUtil.getInstance(activity).getString(Constant.TOKEN,"").equals("")){
                        Intent intent = new Intent(activity, LoginActivity.class);
                        activity.startActivity(intent);
                    }else{
                        if(list.get(position).getGoods().get(i).isPaid()){
                            Intent intent = new Intent(activity, SimulationPracticeActivity.class);
                            intent.putExtra("id", list.get(position).getGoods().get(i).getCollege_id());
                            intent.putExtra("name", list.get(position).getName());
                            intent.putExtra("rank_id",list.get(position).getGoods().get(i).getRank().getId());
                            activity.startActivity(intent);
                        }else{
                            if(list.get(position).getGoods().get(i).getRank().getName().equals("在线套题")){
                                Intent intent = new Intent(activity, OnlineExercisesActivity.class);
                                intent.putExtra("college_id",list.get(position).getGoods().get(i).getRank().getId());
                                intent.putExtra("name","中央音乐学院");
                                activity.startActivity(intent);
                            }else{
                                Intent intent = new Intent(activity, PayDetailActivity.class);
                                intent.putExtra("goods_id",String.valueOf(list.get(position).getGoods().get(i).getId()));
                                intent.putExtra("college_id",String.valueOf(list.get(position).getGoods().get(i).getCollege_id()));
                                intent.putExtra("name",list.get(position).getGoods().get(i).getName());
                                intent.putExtra("uname",list.get(position).getGoods().get(i).getCollege().getName());
                                intent.putExtra("desc",list.get(position).getGoods().get(i).getDesc());
                                intent.putExtra("price",String.format("%.2f", Double.valueOf(list.get(position).getGoods().get(i).getPrice())));
                                intent.putExtra("flag","1");
                                activity.startActivity(intent);
                            }
                        }
                    }
                }else{
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });
    }
}
